#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags)
{
    ui.setupUi(this);
    this->init_tray_icon();

    this->show_message( tr("Linux rulz!") );
}

/* Methods for working with tray icon */

void MainWindow::init_tray_icon() {
    this->tray_icon = new QSystemTrayIcon(this);
    tray_icon->setToolTip( tr("Traffic control") );

    QMenu* menu = new QMenu;

    QAction* show_traff = new QAction(QIcon("images/stats.png"), tr("show traffic"), menu);
    menu->addAction(show_traff);

    menu->addSeparator();

    QAction* exit_action = new QAction(QIcon("images/exit.png"),tr("Exit"), menu);
    menu->addAction(exit_action);

    tray_icon->setContextMenu(menu);

    this->change_tray_status(false);
    tray_icon->show(); // active by default
}

void MainWindow::change_tray_status(bool status) {
    if (this->tray_icon) {
        QIcon icon_for_tray;

        if (status) {
            icon_for_tray.addFile("images/online.png");
        } else {
            icon_for_tray.addFile("images/offline.png");
        }

        this->tray_icon->setIcon(icon_for_tray);
    }
}

void MainWindow::show_message(QString message_text, QString title) {
    if (QSystemTrayIcon::supportsMessages() && this->tray_icon) {
        this->tray_icon->showMessage(title, message_text);
    }
}

/* end methods for working with tray icon */

MainWindow::~MainWindow()
{

}
